﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using ControlModule.Models;
using ControlModule.Security;

namespace ControlModule.Daos
{
    public class FullLabworkDao
    {
        public static Labwork GetLabworkById(int labworkId)
        {
            using (CmDbEntities db = new CmDbEntities())
                return db.Labwork.FirstOrDefault(l => l.Id == labworkId);
        }

        public static Point GetStatusOfFinishedQuiz(int labworkId, int userId)
        {
            int total = 0;
            int successful = 0;
            using (CmDbEntities db = new CmDbEntities())
            {
                total = (
                    from rluqa in db.RelLabworkUserQuestionAnswer
                    where rluqa.LabworkId == labworkId
                    where rluqa.UserId == userId
                    select rluqa).Count();

                successful = (
                    from rluqa in db.RelLabworkUserQuestionAnswer
                    from rqu in db.RelQuestionAnswer
                    where rluqa.LabworkId == labworkId
                    where rluqa.UserId == userId
                    where rluqa.QuestionId == rqu.QuestionId
                    where rluqa.AnswerId == rqu.AnswerId
                    where rqu.IsTrueAnswer == true
                    select rluqa).Count();
            }
            return new Point(total, successful);
        }

        public static bool IfQuizofLabWorkComplete(int labworkId, int userId)
        {
            int pruf = 0;
            using (CmDbEntities db = new CmDbEntities())
            {
                pruf = (
                    from rluqa in db.RelLabworkUserQuestionAnswer
                    where rluqa.LabworkId == labworkId
                    where rluqa.UserId == userId
                    select rluqa).Count();
            }
            return pruf > 0;
        }

        public static FullLabwork GetFullLabworkById(int labworkId)
        {
            FullLabwork fullLabwork = new FullLabwork(GetLabworkById(labworkId));
            List<LabParameter> labParameters = LabParameterDao.LoadParametersByLabworkId(labworkId);

            fullLabwork.FullLabParameters = new List<FullLabParameter>();
            for (int exprIndex = 0; exprIndex < fullLabwork.CountExperiments; exprIndex++)
                foreach (LabParameter labParameter in labParameters)
                    fullLabwork.FullLabParameters.Add(new FullLabParameter(labParameter, exprIndex));

            return fullLabwork;

        }

        public static void SaveFinishedLabwork(FullLabwork fullLabwork)
        {
            using (CmDbEntities db = new CmDbEntities())
            {
                foreach (FullLabParameter fullLabParameter in fullLabwork.FullLabParameters)
                {
                    db.AddToRelLabworkParameterUser(
                    new RelLabworkParameterUser 
                    {
                        LabworkId = fullLabwork.Id,
                        ParameterId = fullLabParameter.Id,
                        Experiment = fullLabParameter.ExperimentIndex,
                        Value = fullLabParameter.Content,
                        UserId = MyMembershipProvider.GetCurrentUserInstance().Id
                    });
                }
                db.SaveChanges();
            }
        }
    }
}